//
//  ZMNavigationBar.m
//  ZMKitDemo
//
//  Created by zm on 2018/6/19.
//  Copyright © 2018年 zm. All rights reserved.
//

#import "ZMNavigationBar.h"

@implementation ZMNavigationBar

- (void)setTitle:(NSString *)title {
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        UILabel *label = (UILabel *)self.titleView;
        label.text = title;
    }
}

- (void)setTitleView:(UIView *)titleView {
    titleView.frame = _titleView.frame;
    [_titleView removeFromSuperview];
    [self addSubview:titleView];
    [self sendSubviewToBack:titleView];
    _titleView = titleView;
}

- (void)setLeftItem:(UIButton *)leftItem {
    leftItem.frame = _leftItem.frame;
    [_leftItem removeFromSuperview];
    [self addSubview:leftItem];
    [self bringSubviewToFront:leftItem];
    _leftItem = leftItem;
    [_leftItem addTarget:self action:@selector(onClickLeft:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setRightItem:(UIButton *)rightItem {
    if (_rightItem) {
        rightItem.frame = _rightItem.frame;
    } else {
        rightItem.frame = CGRectMake(SCREEN_WIDTH - 16 - 30, STATUS_HEIGHT, 30, NAV_HEIGHT);
    }
    [_rightItem removeFromSuperview];
    [self addSubview:rightItem];
    [self bringSubviewToFront:rightItem];
    _rightItem = rightItem;
    [_rightItem addTarget:self action:@selector(onClickRight:) forControlEvents:UIControlEventTouchUpInside];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, TOP_HEIGHT);
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    _titleView = ({
        UILabel *label = [UILabel new];
        label.frame = CGRectMake(0, STATUS_HEIGHT, SCREEN_WIDTH, NAV_HEIGHT);
        label.textColor = UIColorFromRGB(0x3B424C);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        [self addSubview:label];
        label;
    });
    _leftItem = ({
        UIButton *button = [UIButton new];
        button.frame = CGRectMake(0, STATUS_HEIGHT, NAV_HEIGHT, NAV_HEIGHT);
        [button setImage:[UIImage imageNamed:@"zm_back_black"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(onClickLeft:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button;
    });
    
    self.headLine = ({
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT - .5, SCREEN_WIDTH, .5)];
        v.backgroundColor = UIColorFromRGB(0xe4e4e4);
        v.hidden = YES;
        [self addSubview:v];
        v;
    });
}

- (void)onClickLeft:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(navigationBar:onClickLeftItem:)]) {
        [self.delegate navigationBar:self onClickLeftItem:sender];
    }
}

- (void)onClickRight:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(navigationBar:onClickRightItem:)]) {
        [self.delegate navigationBar:self onClickRightItem:sender];
    }
}

@end
